﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;

namespace CS3230HealthTrackerSystemGUI
{
    public partial class RecordAppointment : Form
    {

        private String patientID;
        private String appointmentDate;
        private String appointmentTime;
        private String doctorID;
        private String nurseID;
        private String symptoms;
        private String diagnosis;
        private String pulse;
        private String bodyTemperature;
        private String bloodPressure;   

        private Validation validate = new Validation();
        private Decryption key = new Decryption();
        private FieldValueHolder field = new FieldValueHolder();

        public RecordAppointment()
        {
            InitializeComponent();
        }

        private void orderTestButton_Click(object sender, EventArgs e)
        {
            OrderTestForm orderTestMenu = new OrderTestForm();
            //OrderTestForm orderTestMenu = new OrderTestForm(field);
            orderTestMenu.Show();
        }

        private void recordButton_Click(object sender, EventArgs e)
        {

            try
            {
                //Gathers data from each of the text boxes and verify the data is valid. 
                patientID = patientIDTextBox.Text.Trim();
                appointmentDate = dateTextBox.Text;
                appointmentTime = checkInTimeTextBox.Text;
                doctorID = doctorIDTextBox.Text;
                nurseID = nurseIDTextBox.Text;
                symptoms = symptomsTextBox.Text.Trim();
                diagnosis = diagnosisTextBox.Text.Trim();
                pulse = pulseTextBox.Text.Trim();
                bodyTemperature = bodytemperatureTextBox.Text.Trim();
                bloodPressure = bloodPressureTextBox.Text.Trim();

                //Validates all the fields.
                validate.validateID(patientID, "Patient");
                validate.validateID(doctorID, "Doctor");
                validate.validateID(nurseID, "Nurse");
                validate.validateDate(appointmentDate);
                validate.validateTime(appointmentTime);
                validate.validatePulse(pulse);
                validate.validateBodyTemperature(bodyTemperature);
                validate.validateBloodPressure(bloodPressure);

                //field = new FieldValueHolder(doctorID, patientID, nurseID, appointmentDate, appointmentTime);
                //Converts all the information into the appropriate data types.
                int currentPatient = Int32.Parse(patientID);
                int currentDoctor = Int32.Parse(doctorID);
                int currentNurse = Int32.Parse(nurseID);
                int currentPulse = Int32.Parse(pulse);
                float currentTemperature = float.Parse(bodyTemperature);
           
                //Assigns Connection string.
                string MyConString = MyConString = key.returnConnectionString("xxjrmy", "xxjrmy", "xx3kz4z");
                OdbcConnection dataAccess = new OdbcConnection(MyConString);
                //Creates statement to insert all the information into the database.
                string myInsertQuery = "INSERT INTO VISIT (patientVisiting, appointmentDate, checkInTime, doctorDiagonosing, nurseRecording, symptoms, diagnosis, pulse, bodyTemperature, bloodPressure) VALUES " +
                "( '" + currentPatient + "','" + appointmentDate + "','" + appointmentTime +"', '" + currentDoctor + "','" + currentNurse + "' ,'" + symptoms + "', '" + diagnosis + "', '" + currentPulse + "', '" + currentTemperature + "', '" + bloodPressure + "')";

                //Sets up the connection, and Inserts the data.
                OdbcCommand insertCommand = new OdbcCommand(myInsertQuery);
                insertCommand.Connection = dataAccess;
                dataAccess.Open();
                insertCommand.ExecuteNonQuery();

                //Closes the insert command.
                insertCommand.Connection.Close();

                MessageBox.Show("Appointment information recorded.");

                //Clears information from the textboxes.
                patientIDTextBox.Clear();
                dateTextBox.Clear();
                checkInTimeTextBox.Clear();
                doctorIDTextBox.Clear();             
                symptomsTextBox.Clear();
                diagnosisTextBox.Clear();
                pulseTextBox.Clear();
                bodytemperatureTextBox.Clear();
                bloodPressureTextBox.Clear();
            }

           //Throws an exception if something is incorrectly formatted.
            catch (Exception formatException)
            {
                MessageBox.Show(formatException.Message);
            }

        }
    }
}
